var Koa = require("koa");
var app = new Koa();

// 设置静态资源目录
var koaStatic = require("koa-static");
app.use(koaStatic(__dirname + "/public"));

var koaBody = require("koa-body");
app.use(koaBody());

// 跨域设置
var corsFn = require("./utils/cors");
app.use(function (ctx,next) {
  ctx.set("Access-Control-Allow-Origin", "*");
  ctx.set("Access-Control-Allow-Methods", "PUT, POST, GET, DELETE, OPTIONS");
  // 请求头设置
  ctx.set(
    "Access-Control-Allow-Headers",
    `Content-Type, Content-Length, Authorization, Accept, X-Requested-With , yourHeaderFeild,x-token,sessionToken,token`
  );
  if (ctx.method == "OPTIONS") {
    ctx.body = 200;
  } else {
    next();
  }
});

// 添加路由
var router = require("./router");
app.use(router.routes());

app.listen(8888, function () {
  console.log("服务已启动, 在http://localhost:8888");
});
